home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / RAVE SDK 1.06 GM for MacOS / Example Projects / RaveTest / RAVE_Test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-16  |  10.6 KB  |  508 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2.  **                                                                             **
  3.  **     Module:        RAVE_Test.c                                                 **
  4.  **                                                                          **
  5.  **     Purpose:     Main file for RAVE test app                                 **
  6.  **                                                                          **
  7.  **     Author:        Mike W. Kelley                                             **
  8.  **                                                                             **
  9.  **                    2/3/95    Revised for 0.9 SDK release                         **
  10.  **                    2/23/96 BHP - Revised for 1.05 relase                     **
  11.  **                                                                          **
  12.  **     Copyright (C) 1994-95 Apple Computer, Inc.  All rights reserved.     **
  13.  **                                                                          **
  14.  *****************************************************************************/
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <math.h>
  19.  
  20. /* Macintosh */
  21. #include <Types.h>
  22. #include <QuickDraw.h>
  23. #include <Windows.h>
  24.  
  25. /* Project */
  26. #include "RAVE.h"
  27. #include "RAVE_Test.h"
  28.  
  29. #include "QD3DAcceleration.h"
  30.  
  31. /*******************************************************************************************
  32.  *
  33.  * Defines that affect how the tests are run.
  34.  *
  35.  ******************************************************************************************/
  36.  
  37.  #define dWaitForButton        /* If defined, wait for button click between tests */
  38.  
  39. /*******************************************************************************************
  40.  *
  41.  * Globals.
  42.  *
  43.  ******************************************************************************************/
  44.  
  45. long            gWindowWidth, gWindowHeight;
  46. TQAEngine        *gEngine [kRAVE_Windows];
  47. TQADrawContext    *gDrawContext [kRAVE_Windows];
  48. TQATexture        *gTexture [kRAVE_Windows];
  49.  
  50. /*******************************************************************************************
  51.  *
  52.  * Static variables used by this file's functions only.
  53.  *
  54.  ******************************************************************************************/
  55.  
  56. static float frand (void)
  57. {
  58.     return ((Random() + 32768) / 65536.0);
  59. }
  60.  
  61. /*******************************************************************************************
  62.  *
  63.  * Main. This runs the tests, clearing the windows before each test and waiting for
  64.  * a mouse click after each test. Each test is run four ways: Single and double buffered,
  65.  * with and without 2D clipping.
  66.  *
  67.  ******************************************************************************************/
  68.  
  69. int main()
  70. {
  71.     long        tinselTest, tinselStyle;
  72.     long        i;
  73.     TQAVGouraud    vGouraud[3];
  74.     TQAVTexture    vTexture[3];
  75.     long        flushCount;
  76.     EventRecord theEvent;
  77.     
  78.     /*
  79.      * If we are running with a shared library, TdLinkEngine should not be defined!
  80.      */
  81.     
  82.     #ifdef dLinkEngine
  83.         (void) TtRegister();
  84.     #endif
  85.     
  86.     /*
  87.      * Enable the Apple Hardware Engine
  88.      */
  89.     
  90.         QAEngineEnable(kQAVendor_Apple, kQAEngine_AppleHW);
  91.         
  92.     /*
  93.      * Create the test window(s).
  94.      */
  95.     
  96.     InitToolbox ();
  97.  
  98.     if ( ! CreateTestWindows (kRAVE_MaxWindowSize, kRAVE_MaxWindowSize))
  99.     {
  100.         return (-1);
  101.     }
  102.     if ( ! CreateWindowClip ())
  103.     {
  104.         return (-1);
  105.     }
  106.     if ( ! TestTextureNew())
  107.     {
  108.         return (-1);
  109.     }
  110.     
  111.     /*
  112.      * Run tests.
  113.      */
  114.     
  115.     for (tinselStyle = 0; tinselStyle <= kMaxStyle; ++tinselStyle)
  116.     {
  117.         switch (tinselStyle)
  118.         {
  119.             case kStyleSingleBuffered:
  120.                 if ( ! TestDrawContextNew (kQAContext_DeepZ, false))
  121.                 {
  122.                     return (-1);
  123.                 }
  124.                 break;
  125.             
  126.             case kStyleDoubleBuffered:
  127.                 if ( ! TestDrawContextNew (kQAContext_DeepZ | kQAContext_DoubleBuffer, false))
  128.                 {
  129.                     return (-1);
  130.                 }
  131.                 break;
  132.                 
  133.             case kStyleSingleBufferedClip:
  134.                 if ( ! TestDrawContextNew (kQAContext_DeepZ, true))
  135.                 {
  136.                     return (-1);
  137.                 }
  138.                 break;
  139.             
  140.             case kStyleDoubleBufferedClip:
  141.                 if ( ! TestDrawContextNew (kQAContext_DeepZ | kQAContext_DoubleBuffer, true))
  142.                 {
  143.                     return (-1);
  144.                 }
  145.                 break;
  146.         }
  147.  
  148.         ClearWindows();
  149.             
  150.         for (tinselTest = 0; tinselTest <= kMaxTest; ++tinselTest)
  151.         {
  152.             switch (tinselTest)
  153.             {
  154.                 case kTestPoints:
  155.                     TestRenderStart();
  156.                     for (i = 0, flushCount = kNPoints / kNFlushes; i++ < kNPoints; /* Nothing */)
  157.                     {
  158.                         TestRandomVGouraud (&vGouraud[0]);
  159.                         TestDrawPoint (&vGouraud[0]);
  160.                         if ((i % flushCount) == 0)
  161.                         {
  162.                             TestFlush();
  163.                         }
  164.                     }
  165.                     TestRenderEnd();
  166.                     break;
  167.                     
  168.                 case kTestLines:
  169.                     TestRenderStart();
  170.                     for (i = 0, flushCount = kNLines / kNFlushes; i++ < kNLines; /* Nothing */)
  171.                     {
  172.                         TestRandomVGouraud (&vGouraud[0]);
  173.                         TestRandomVGouraud (&vGouraud[1]);
  174.                         TestDrawLine (&vGouraud[0], &vGouraud[1]);
  175.                         if ((i % flushCount) == 0)
  176.                         {
  177.                             TestFlush();
  178.                         }
  179.                     }
  180.                     TestRenderEnd();
  181.                     break;
  182.                     
  183.                 case kTestTriGouraud:
  184.                     TestRenderStart();
  185.                     for (i = 0, flushCount = kNTris / kNFlushes; i++ < kNTris; /* Nothing */)
  186.                     {
  187.                         TestRandomVGouraud (&vGouraud[0]);
  188.                         TestRandomVGouraud (&vGouraud[1]);
  189.                         TestRandomVGouraud (&vGouraud[2]);
  190.                         
  191.                         TestDrawTriGouraud (&vGouraud[0],
  192.                                 &vGouraud[1], &vGouraud[2], kQATriFlags_None);
  193.                         
  194.                         if ((i % flushCount) == 0)
  195.                         {
  196.                             TestFlush();
  197.                         }
  198.                     }
  199.                     TestRenderEnd();
  200.                     break;
  201.                     
  202.                 case kTestTriTexture:
  203.                     TestRenderStart();
  204.                     TestSetStateTexture();
  205.                     for (i = 0, flushCount = kNTris / kNFlushes; i++ < kNTris; /* Nothing */)
  206.                     {
  207.                         /*
  208.                          * Setup the u,v values before calling TestRandomVTexture(),
  209.                          * as it references them.
  210.                          */
  211.                         
  212.                         vTexture[0].uOverW = 0.0;
  213.                         vTexture[0].vOverW = 0.0;
  214.  
  215.                         vTexture[1].uOverW = 0.0;
  216.                         vTexture[1].vOverW = 1.0;
  217.  
  218.                         vTexture[2].uOverW = 1.0;
  219.                         vTexture[2].vOverW = 1.0;
  220.  
  221.                         TestRandomVTexture (&vTexture[0]);
  222.                         TestRandomVTexture (&vTexture[1]);
  223.                         TestRandomVTexture (&vTexture[2]);
  224.                         
  225.                         TestDrawTriTexture (&vTexture[0],
  226.                                 &vTexture[1], &vTexture[2], kQATriFlags_None);
  227.                         
  228.                         if ((i % flushCount) == 0)
  229.                         {
  230.                             TestFlush();
  231.                         }
  232.                     }
  233.                     TestRenderEnd();
  234.                     break;
  235.             }
  236.             
  237.             #ifdef dWaitForButton
  238.                 /*
  239.                  * Wait for a mouse button click..
  240.                  */
  241.                 
  242.                 while ( ! Button())
  243.                     GetNextEvent(everyEvent, &theEvent);
  244.                     /* Do nothing */ ;
  245.                 FlushEvents (everyEvent, 0);
  246.             #endif
  247.         }
  248.         
  249.         TestDrawContextDelete();
  250.     }
  251.     
  252.     return (0);
  253. }
  254.  
  255. /*******************************************************************************************
  256.  *
  257.  * Set xyzw fields of a TQAVGouraud randomly. All other fields get simple defaults.
  258.  *
  259.  ******************************************************************************************/
  260.  
  261. void TestRandomVGouraud (
  262.     TQAVGouraud        *v)
  263. {
  264.     v->x = 100000.0;/*frand() * gWindowWidth;*/
  265.     v->y = frand() * gWindowHeight;
  266.     v->z = frand();
  267.     v->invW = 1.0 / v->z;
  268.     
  269.     v->a = 1.0;
  270.     v->r = frand();
  271.     v->g = frand();
  272.     v->b = frand();
  273. }
  274.  
  275. /*******************************************************************************************
  276.  *
  277.  * Set xyzw fields of a TQAVTexture randomly. The incoming u,v values are multiplied
  278.  * by the random invW value; all other fields get simple defaults.
  279.  *
  280.  ******************************************************************************************/
  281.  
  282. void TestRandomVTexture (
  283.     TQAVTexture        *v)
  284. {
  285.     v->x = frand() * gWindowWidth;
  286.     v->y = frand() * gWindowHeight;
  287.     v->z = frand();
  288.     v->invW = 1.0 / v->z;
  289.     
  290.     v->uOverW *= v->invW;
  291.     v->vOverW *= v->invW;
  292.     
  293.     v->a = 1.0;
  294.     v->r = 0.0;
  295.     v->g = 0.0;
  296.     v->b = 0.0;
  297.     
  298.     v->kd_r = 1.0;
  299.     v->kd_g = 1.0;
  300.     v->kd_b = 1.0;
  301.     
  302.     v->ks_r = 0.0;
  303.     v->ks_g = 0.0;
  304.     v->ks_b = 0.0;
  305. }
  306.  
  307. /*******************************************************************************************
  308.  *
  309.  * Miscellaneous test functions.
  310.  *
  311.  ******************************************************************************************/
  312.  
  313. void TestRenderStart (void)
  314. {
  315.     long        i;
  316.     
  317.     for (i = 0; i < kRAVE_Windows; ++i)
  318.     {
  319.         if (gDrawContext [i])
  320.         {
  321.             QARenderStart (gDrawContext [i], NULL, NULL);
  322.         }
  323.     }
  324. }
  325.  
  326. void TestRenderEnd (void)
  327. {
  328.     long        i;
  329.     TQAError    status;
  330.     
  331.     for (i = 0; i < kRAVE_Windows; ++i)
  332.     {
  333.         if (gDrawContext [i])
  334.         {
  335.             status = QARenderEnd (gDrawContext [i], NULL);
  336.         }
  337.     }
  338. }
  339.  
  340. void TestSetStateTexture (void)
  341. {
  342.     long        i;
  343.     
  344.     for (i = 0; i < kRAVE_Windows; ++i)
  345.     {
  346.         if (gDrawContext [i])
  347.         {
  348.             QASetPtr (gDrawContext [i], kQATag_Texture, gTexture [i]);
  349.             
  350.             /*
  351.              * NOTE: The current Apple software rasterizer always assumes these
  352.              * kQATextureOp_Modulate | kQATextureOp_Highlight are set, as that's
  353.              * the standard QuickDrawâ„¢ 3D mode. So if you change this, you may
  354.              * find that the software rasterizer isn't a good test case.
  355.              * (This will be fixed soon...)
  356.              */
  357.             
  358.             QASetInt (gDrawContext [i], kQATag_TextureOp,
  359.                     kQATextureOp_Modulate | kQATextureOp_Highlight);
  360.         }
  361.     }
  362. }
  363.  
  364. void TestFlush (void)
  365. {
  366.     long        i;
  367.     
  368.     for (i = 0; i < kRAVE_Windows; ++i)
  369.     {
  370.         if (gDrawContext [i])
  371.         {
  372.             QAFlush (gDrawContext [i]);
  373.         }
  374.     }
  375. }
  376.  
  377. void TestDrawPoint (
  378.     const TQAVGouraud    *v)
  379. {
  380.     long        i;
  381.     
  382.     for (i = 0; i < kRAVE_Windows; ++i)
  383.     {
  384.         if (gDrawContext [i])
  385.         {
  386.             QADrawPoint (gDrawContext [i], v);
  387.         }
  388.     }
  389. }
  390.  
  391. void TestDrawLine (
  392.     const TQAVGouraud    *v0,
  393.     const TQAVGouraud    *v1)
  394. {
  395.     long        i;
  396.     
  397.     for (i = 0; i < kRAVE_Windows; ++i)
  398.     {
  399.         if (gDrawContext [i])
  400.         {
  401.             QADrawLine (gDrawContext [i], v0, v1);
  402.         }
  403.     }
  404. }
  405.  
  406. void TestDrawTriGouraud (
  407.     const TQAVGouraud    *v0,
  408.     const TQAVGouraud    *v1,
  409.     const TQAVGouraud    *v2,
  410.     unsigned long        flags)
  411. {
  412.     long        i;
  413.     
  414.     for (i = 0; i < kRAVE_Windows; ++i)
  415.     {
  416.         if (gDrawContext [i])
  417.         {
  418.             QADrawTriGouraud (gDrawContext [i], v0, v1, v2, flags);
  419.         }
  420.     }
  421. }
  422.  
  423. void TestDrawTriTexture (
  424.     const TQAVTexture    *v0,
  425.     const TQAVTexture    *v1,
  426.     const TQAVTexture    *v2,
  427.     unsigned long        flags)
  428. {
  429.     long        i;
  430.     
  431.     for (i = 0; i < kRAVE_Windows; ++i)
  432.     {
  433.         /*
  434.          * Test both for a draw context, and that a texture was created.
  435.          */
  436.         
  437.         if (gDrawContext [i] && gTexture [i])
  438.         {
  439.             QADrawTriTexture (gDrawContext [i], v0, v1, v2, flags);
  440.         }
  441.     }
  442. }
  443.  
  444. Boolean TestTextureNew (void)
  445. {
  446.     TexturePixel        *pixmap;
  447.     long                x, y;
  448.     long                i;
  449.     
  450.     /*
  451.      * Create the source pixmap. Note that we can't free it (unless we
  452.      * called QATextureDetach()). We just do a procedural map with some
  453.      * checkerboard effect...
  454.      */
  455.     
  456.     if ( ! (pixmap = (TexturePixel *) malloc (sizeof (*pixmap) * kTextureSize * kTextureSize)))
  457.     {
  458.         return (false);
  459.     }
  460.     
  461.     for (x = 0; x < kTextureSize; ++x)
  462.     {
  463.         for (y = 0; y < kTextureSize; ++y)
  464.         {
  465.             pixmap [x + (y << kTextureSizeNBits)].a = 0xff;
  466.             pixmap [x + (y << kTextureSizeNBits)].r = (x & 0x1f) << 3;
  467.             pixmap [x + (y << kTextureSizeNBits)].g = (y & 0x1f) << 3;
  468.             pixmap [x + (y << kTextureSizeNBits)].b = (((x + y) / 2) & 0x1f) << 3;
  469.         }
  470.     }
  471.  
  472.     for (i = 0; i < kRAVE_Windows; ++i)
  473.     {
  474.         if (gEngine [i])
  475.         {
  476.             /*
  477.              * Create the texture. If this fails, gTexture[i] will be NULL.
  478.              * This is ok, as TestDrawTriTexture() always tests it before calling
  479.              * QADrawTriTexture().
  480.              */
  481.             
  482.             gTexture [i] = CreateMipMapTexture (
  483.                     gEngine [i],                                /* store context */
  484.                     pixmap,                                        /* high res pix map */
  485.                     kTextureSize * sizeof (*pixmap),            /* row bytes */
  486.                     kTextureSizeNBits,                            /* widthNBits */
  487.                     kTextureSizeNBits);                            /* heightNBits */
  488.         }
  489.     }
  490.     
  491.     return (true);
  492. }
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.